/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pehulja.mmdo.coursework.gui;

import com.pehulja.mmdo.coursework.gui.graph.JPanelGraph;
import com.pehulja.mmdo.coursework.gui.graph.Line;
import com.pehulja.mmdo.coursework.gui.graph.Point;
import com.pehulja.mmdo.coursework.math.Solver;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.List;
import org.apache.commons.math.linear.ArrayRealVector;

/**
 *
 * @author pehulja
 */
public class StabilityAreaDialog extends javax.swing.JDialog {

    /**
     * Creates new form StabilityAreaDialog
     */
    private Solver solver;
    private JPanelGraph graph;
    private boolean isDragged;
    public StabilityAreaDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        customInitComponets();
    }

    private void customInitComponets() {
        final Point start = new Point();
        graph = new JPanelGraph();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(graph);
        graph.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 591, Short.MAX_VALUE));
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 428, Short.MAX_VALUE));

        graph.setLayout(new GridBagLayout());
        graph.setBounds(new Rectangle(0, 0, this.getWidth(), this.getHeight()));
        graph.addMouseListener(new java.awt.event.MouseListener() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void mouseEntered(MouseEvent arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void mouseExited(MouseEvent arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void mousePressed(MouseEvent arg0) {
                // TODO Auto-generated method stub
                isDragged = true;
                start.setX(arg0.getX());
                start.setY(arg0.getY());
            }

            @Override
            public void mouseReleased(MouseEvent arg0) {
                // TODO Auto-generated method stub
                isDragged = false;
            }

        });
        graph.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent arg0) {
                // TODO Auto-generated method stub
                graph.horisontalMove(start.getX() - arg0.getX());
                graph.verticalMove(start.getY() - arg0.getY());
                start.setX(arg0.getX());
                start.setY(arg0.getY());
                graph.revalidate();
                graph.repaint();
            }

            @Override
            public void mouseMoved(MouseEvent arg0) {
                // TODO Auto-generated method stub
            }
        });
        graph.addMouseWheelListener(new MouseWheelListener() {

            public void mouseWheelMoved(MouseWheelEvent e) {
                int steps = e.getWheelRotation();
                if(steps>0)
                    graph.PlusScale();
                else
                    graph.MinusScale();
                graph.revalidate();
                graph.repaint();
            }
               
        });
    

    this.add(graph);

}
/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 615, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 527, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                

}
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StabilityAreaDialog.class  

.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 

catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StabilityAreaDialog.class  

.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 

catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StabilityAreaDialog.class  

.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } 

catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StabilityAreaDialog.class  

.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                StabilityAreaDialog dialog = new StabilityAreaDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
        public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    public void setSolver(Solver solver) {
        this.solver = solver;
        List <Line> lines = solver.getGraphs();
        for(Line line:lines)   
            graph.addLine(line);
        this.addPoints(solver.getCrossingOfLinePoints());
        graph.revalidate ();
        graph.repaint ();
    }
    public void addPoint(double x, double y) {
        graph.addPoint(x, y);
 
    }
    public void addPoints(List<ArrayRealVector> points) {
        for(ArrayRealVector vector:points)
            graph.addPoint(vector.getEntry(0), vector.getEntry(1));
    }

    public JPanelGraph getGraph() {
        return graph;
    }
    
}
